# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface

from FetchFund import settings
from peewee import *

db = MySQLDatabase(settings.MYSQL_DBNAME, host=settings.MYSQL_HOST, port=settings.MYSQL_PORT, user=settings.MYSQL_USER, password=settings.MYSQL_PASSWD)

class MysqlPeeweePipeline:
    def __init__(self):
        Fund.drop_table()
        Fund.create_table()
        
        self.data=[]
        self.codeDict={}
        pass
    
    def process_item(self, item, spider):
        self.codeDict[item['fund_code']]=item['fund_code']
        self.data.append((item['fund_code'],item['fund_name'],item['fund_link'],item['fund_current_value'],item['fund_current_date'],item['fund_total_value'],
                 item['one_month_rate'],item['three_month_rate'],item['six_month_rate'],item['one_year_rate'],item['three_year_rate'],item['total_rate'],
                 item['fund_start_time'],item['fund_manager'],item['fund_size'],item['fund_type'],item['fund_level']))
        if len(self.data)>300:
            self._write2db()
        return item
    
    def close_spider(self, spider):
        if len(self.data)>0:
            self._write2db()
        #print(len(self.codeDict))
        return
    
    def _write2db(self):
        with db.atomic():
            Fund.insert_many(self.data).execute()
        self.data.clear()


class Fund(Model):
    id = AutoField(primary_key=True)
    fund_code = CharField(max_length=16,null=True)
    fund_name = CharField(max_length=64,null=True)
    fund_link = CharField(max_length=255,null=True)
    fund_current_value = CharField(max_length=64,null=True)
    fund_current_date = CharField(max_length=32,null=True)
    fund_total_value = CharField(max_length=32,null=True)
    one_month_rate = CharField(max_length=16,null=True)
    three_month_rate = CharField(max_length=16,null=True)
    six_month_rate = CharField(max_length=16,null=True)
    one_year_rate = CharField(max_length=16,null=True)
    three_year_rate = CharField(max_length=16,null=True)
    total_rate = CharField(max_length=16,null=True)
    fund_start_time = CharField(max_length=32,null=True)
    fund_manager = CharField(max_length=32,null=True)
    fund_size = CharField(max_length=32,null=True)
    fund_type = CharField(max_length=32,null=True)
    fund_level = CharField(max_length=32,null=True)


    class Meta:
        database = db
        table_name = 't_fund'


